package main.java.com.tenniskata;

import java.util.HashMap;
import java.util.Map;

import main.java.com.tenniskata.exception.BadRangeException;
import main.java.com.tenniskata.exception.OddSetsException;
import main.java.com.tenniskata.exception.WrongInputException;

public class Score {

	private int firstPlayerSets = 0;
	private int secondPlayerSets = 0;
	private Map<Integer, Integer> firstPlayerGames = new HashMap<>();
	private Map<Integer, Integer> secondPlayerGames = new HashMap<>();
	
	public Score(int... sets) throws OddSetsException, BadRangeException, WrongInputException {
		if(isOdd(sets))
			throw new OddSetsException();
		setAndCountSets(sets);
	}
	
	

	public String getScoreString() {
		return getFirstPlayerSets() + ":" + getSecondPlayerSets();
	}
	
	private void setAndCountSets(int[] sets) throws BadRangeException, WrongInputException {
		for(int i = 0 ; i < sets.length ; i+=2) {
			int firstPlayerSetGames = sets[i];
			int secondPlayerSetGames = sets[i+1];
			if(firstPlayerSetGames < 0 || secondPlayerSetGames < 0) 
				throw new BadRangeException();
			int setNumber = i / 2 + 1;
			firstPlayerGames.put(setNumber, firstPlayerSetGames);
			secondPlayerGames.put(setNumber, secondPlayerSetGames);
			if((firstPlayerSetGames == secondPlayerSetGames) 
					&& ((i + 2) < sets.length)) 
				throw new WrongInputException();
			checkPosibleResult(firstPlayerSetGames,secondPlayerSetGames);
			firstPlayerSets += oneIfFirstPlayerWonSet(firstPlayerSetGames, secondPlayerSetGames);
			secondPlayerSets += oneIfFirstPlayerWonSet(secondPlayerSetGames, firstPlayerSetGames);
		}
	}
	
	private int oneIfFirstPlayerWonSet(int firstPlayerSetGames, int secondPlayerSetGames) throws WrongInputException {
		if(firstPlayerSetGames == secondPlayerSetGames) return 0;
		if(firstPlayerSetGames < 6 && secondPlayerSetGames < 6) return 0;
		if(firstPlayerSetGames == 6 && secondPlayerSetGames == 6) return 0;
		if(firstPlayerSetGames == 7 && secondPlayerSetGames == 6) return 1;
		if(firstPlayerSetGames == 6 && secondPlayerSetGames == 7) return 0;
		if(firstPlayerSetGames > secondPlayerSetGames) return 1;
		if(firstPlayerSetGames < secondPlayerSetGames) return 0;
		throw new WrongInputException();
	}

	private void checkPosibleResult(int firstPlayerSetGames,
			int secondPlayerSetGames) throws WrongInputException {
		if(firstPlayerSetGames > 6 || secondPlayerSetGames > 6) {
			if(Math.abs(firstPlayerSetGames - secondPlayerSetGames) > 2 ) 
				throw new WrongInputException();
		}	
	}

	private boolean isOdd(int[] sets) {
		int numberOfSets = sets.length;
		return (((numberOfSets % 2) == 0) ? false : true); 
	}
	
	public int getFirstPlayerSets() {
		return firstPlayerSets;
	}
	
	public int getSecondPlayerSets() {
		return secondPlayerSets;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append(firstPlayerSets);
		builder.append(':');
		builder.append(secondPlayerSets);
		builder.append('(');
		for(int i = 1 ; i <= firstPlayerGames.size() ; i++) {
			if(i != 1) builder.append(' ');
			builder.append(firstPlayerGames.get(i));
			builder.append(secondPlayerGames.get(i));
		}
		builder.append(')');
		return builder.toString();
	}
	
}
